﻿using System;
using Microsoft.Xna.Framework;

namespace GrapplingHookGameData
{
    public class Animation
    {
        private int[] frameIndices;
        private int currentFrame = 0;
        private int elapsedAnimationTime = 0;

        public SpriteSheet SpriteSheet { get; private set; }
        public int Delay { get; private set; }
        public int Frames { get; private set; }
        public string Name { get; private set; }

        public Animation(SpriteSheet spriteSheet, string baseName, int numFrames, int delay)
        {
            SpriteSheet = spriteSheet;
            frameIndices = new int[numFrames];
            for( int i=0; i<numFrames;i++)
            {
                frameIndices[i] = spriteSheet.GetIndex(baseName+string.Format("{0:0000}", i));
            }
            Delay = delay;
            Frames = numFrames;
        }

        public Animation(SpriteSheet spriteSheet, string baseName, int numFrames, int delay, string name):this(spriteSheet, baseName, numFrames, delay)
        {
            Name = name;
        }

        public int Start()
        {
            elapsedAnimationTime = 0;
            currentFrame = 0;
            return frameIndices[currentFrame];
        }

        public int Update(GameTime gameTime)
        {
            elapsedAnimationTime += gameTime.ElapsedGameTime.Milliseconds;
            if (elapsedAnimationTime > Delay)
            {
                currentFrame++;
                elapsedAnimationTime = 0;
                if (currentFrame >= frameIndices.Length) currentFrame = 0;
            }
            return frameIndices[currentFrame];
        }

    }
}
